

%MACRO FEREG0(y, x, firm, time, cluster,dataset, output );
data datatemp;
set &dataset;
run;

proc sort data=datatemp;
by &firm;
run;
/*
proc standard data=datatemp out=datatemp mean=0;
by &firm;
var &x;
run;
*/

/*** the coefficents are the same as the standard fe but the s.e may be slightly undereatimate ****/
/*** using the proc gml absorb insted of the 2-step method can get the correct coefs and s.e   ****/

ods output ParameterEstimates=tempout(keep=variable estimate tvalue probt rename =(variable = parameter));
proc reg data=datatemp;
model &y = &x ;
quit;
ods output close;

ods output DataSummary=tempsummary(keep=Label1 cValue1) FitStatistics=tempFitstat(keep=Label1 cValue1);
proc surveyreg data=datatemp;
model &y = &x ;
quit;
ods output close;

/*
ods output ParameterEstimates=tempout(keep=parameter estimate tvalue probt) DataSummary=tempsummary(keep=Label1 cValue1) FitStatistics=tempFitstat(keep=Label1 cValue1);
proc surveyreg data=datatemp;
cluster &cluster;   
model &y = &x ;
quit;
ods output close;
*/
/*
  proc glm data=datatemp;
      absorb &firm;
      model &y =&x / solution noint;
	  quit;
   run;

*/


data tempcoef(keep=parameter estimate probt) tempt(keep=parameter tvalue);
set tempout;
run;

data tempcoef;
set tempcoef;
_TYPE="coef";
 if probt<0.1 then p='*  ';
 if probt<0.05 then p='** '; 
 if probt<0.01 then p='***';
value=trim(left(put(estimate,10.3)))||trim(left(p));
_N=(_N_-1)*2+1;
keep parameter _TYPE  value _N;
run;

data tempt;
set tempt;
_TYPE="t";
value=trim(left('['||trim(left(put(tvalue,10.3)))||']'));
_N=_N_*2;
keep parameter _TYPE value _N;
run;

data &output;
merge tempcoef tempt;
by _N;
drop _N;
run;

data tempsummary;
set tempsummary;
if _N_=1;  
keep  parameter _type value;
parameter="Nobs";
_type="Nobs";
value=cvalue1;
run;

data tempFitstat;
set tempFitstat;
keep  parameter _type value;
if _N_=1;
parameter="Rsquare";
_type="Rsquare";
value=cvalue1;
run;

data &output;
set &output tempsummary tempFitstat;
run;

proc datasets library=work nolist;
delete datatemp tempcoef tempt tempout;
quit;
run;

%mend FEREG0;
